///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
///<reference path='SPRESTFile.ts'/>
// Module
module SPREST {

    // Class
    export class FileCollection {
        public started: bool = false;
        public ready: bool = false;
        public dfd: JQueryDeferred = $.Deferred();

        public files: SPREST.File[] = null;

        constructor (public Context: SPREST.Context, public Web: SPREST.Web, public Folder: SPREST.Folder, public APIUrl: string) {
        }

        start(): JQueryDeferred {
            var myThis = this;
            if (myThis.started) return myThis.dfd;
            myThis.started = true;
            $.ajax(myThis.APIUrl,
               {
                async: true,
                headers: { "Accept":"application/json;odata=verbose" },
                type: "GET"
            }
            ).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.files = [];
                for (var looper = 0; looper < data.d.results.length; looper++) {
                    myThis.files.push(new SPREST.File(myThis.Context, myThis.Web, myThis.Folder, myThis, data.d.results[looper]));
                }
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        }
    }
}